package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2016110200786867";

    // 商户私钥，您的PKCS8格式RSA2私钥
    public  String merchant_private_key = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDCkBzPBx7oWGZtj/znlZyFex+LLbRSK8LkHQETLq6x0jwTBnmpney4NzxGYBd6JMI8GMqej/vKF1SksGjyWblvxrb5WagXoCLNkD0yzJTVxcysnNvQoGC4LZOElnP5wjq5sOe/+ycw+C1oydNY+3sTYge/SpztvEnhhLjkcwdCmUChetWEz6XKvwd0fSDpkQ+hiHRr0rlgVlXBKM25SfsiC2PYqsptlLmpaEWZKRbAul4Z8Mw25CC3ZeKByhI+AjrS+vVEXT05VqcuRnYrMcmZleObxCm03YrlvTAvvpDg+U45tbYxc/QwRaBnhDq7JKaqDlQaZtumuUgWf5G22CwtAgMBAAECggEBAI2monLP+l6qzZgC9twcfuJnq6WG1vvkRHYT785pdGKeb4Jien1VLx1HiairGSyiSo+6dPpQVb7ikYt9rEqVrH6GrHyKG+lney8yTnwHmmyxH7ykTx1rRj+RXw9vAmzaTisXcwLA9d2fMmBSYH8uSILqIj3RMsObDCcWnA4m+SgwaWNCrg47w9YjfuzQodWKl9zJ9SmySUwrC3EgVVS3sK5SArJ7pc/gANp+tpbJFuN+hnm2vR5J+YirYyycciF3STYXBobXwTMfqWNlw49uWV9zrKCMbFaYY3jmRmK/V8ZuIOfb3gt2MSyPp8ipaJEVP0PORzgaX+UK9zh1zEdkVbkCgYEA8hi1SrM9turGtm09ayNq+iBhlvHHh/GdYrwsRn87sKa8253W6PTBz0GsSWGjCm/+yzXqpdD9wHN+ONHTiFHxgc/N1Uv5jiRSWHFyynPBUtvcN6VyVuTSFzBjMLMHdxzK3iDTom5ZsqWNQqw3yKfDCpzmWpW/oth9M0vSx2h92WMCgYEAzbyRYmx6XkqPhsa3pzutw9OTFWKRcDb/ISj/4muobbXOaf0BveRQNoaDEzRcHVo4wdX4nT6kOQqb+xF5XL8r7o+8NBSDRksE8ttDbMOxs5sth5WRHlnEo++AhlKLoM/r02tzcfg9Pr905qcFqPMKTbOcmaChTHIbm+dftVfCoS8CgYEApOX9fg8OZywNA67uma47LHmn3yd6L/d7qwfe5Hm/T3iSxMgC3RPxetkRn734TqCwHPvxgtYSMOa5v7vz6DNgW3oOik1Agvfser9zX8BumqCUo/Z2JgefrhnQRhh7aj9bUudrH4UoZY1r5VDmgi76o5zxLhw3tpTc2na38AkVR18CgYEAoZR6nlFSHeHzLUe+j1IYm9b8CmyaiwY2g8WeiiZBZsDF94nkp028DcbBBQE3Yzorn1i8ZEsBUWdar+m+ZjAigb9TcZe2ritXHKFGKBLd6pByRq0aBzs7bAE68MO9s7P4o9gdGCJ9f12Ao3IDyPfwXKw2zpVQeSiZNG2YRmVBj7MCgYBloS7Tvu7MIRWSaZhebIiRRAgur1yHuOHK4s9hMZqZeFS3pi80ecX2Ezc+WQ0MrdOL2/r3yE5K1GS6teGuvGW/ufDqanP459xc11io3s8nwBIKIKqhLQluApahJmRnrh2/BIQhy28b9V0+UJzHaV26t6qMTrp9HfKa3ebmwqqWdA==";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAj9C8ftTTibLpmgZp+u6Nh0Izh6+XUyosuU7ZkEfTYy0W1ZSyp2n17AVs8t+llY5fJtxSrZ6qz5+ZkoAbM6bu7sInygZTAUHh686cpOOaaW7H3nGIJTtAmnaZVNWxTJrn6+gwWL4rH36/hKEkk8tm/JUtxh6/WM5qKFBPd/yIP7n7IM0FAYOtL0J1minc/YkNiurMCrJ11/TSRByGdrBRVqujmlXzG028HknYbTs3ck3MgFp8xjH5N2qOedoAQMCiRSYBjueLtzPH+cRNgJdIIr75LFIg3Z7gs1qPoKaHVTbV6oz8rbNgAPdyyEmaYV3Mm0X1TAj625doN3xJcPyiPwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    public  String notify_url = "http://dtwsj3fbu4.52http.net/alipay.trade.page.pay-JAVA-UTF-8/notify_url.jsp";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    public  String return_url = "http://localhost:8000/menberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    public String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
